local kuangli = fk.CreateSkill{
  name = "kl__kuangli",
  tags = { Skill.Compulsory },
}


kuangli:addEffect(fk.EventPhaseStart, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(kuangli.name) and player.phase == Player.Play and
    table.find(player.room:getOtherPlayers(player), function (p) return p:getMark("@@kl__kuangli") == 0 end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player), function(p)
      return p:getMark("@@kl__kuangli") == 0
    end)
    local tos = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 2,
      targets = targets,
      skill_name = kuangli.name,
      prompt = "#kl__kuangli-give",
      cancelable = false,
    })
    room:doIndicate(player, tos)
    for _, p in ipairs(tos) do
      room:setPlayerMark(p, "@@kl__kuangli", 1)
    end
  end,
})

kuangli:addEffect(fk.TargetSpecified, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(kuangli.name) and player.phase == Player.Play and
    not data.to.dead and data.to:getMark("@@kl__kuangli") > 0 and player:usedEffectTimes(self.name, Player.HistoryPhase) < 2
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if not player:isNude() and not player.dead then
      local id = room:askToChooseCard(player, {
        target = player,
        flag = "hej",
        skill_name = kuangli.name,
      })
      room:throwCard(id, kuangli.name, player, player)
    end
    if not data.to:isNude() and not data.to.dead then
      local id = room:askToChooseCard(player, {
        target = data.to,
        flag = "hej",
        skill_name = kuangli.name,
      })
      room:throwCard(id, kuangli.name, data.to, player)
    end
    if not player.dead then
      player:drawCards(2, kuangli.name)
    end
  end,
})


return kuangli
